﻿<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="layout :: common_header(~{::title})">
    <title>库存管理-仓库列表</title>
</head>
<body th:replace="layout :: common_body(~{::div.layui-body})">

<div class="layui-body" style="padding:15px;top: 0px;">

    <!-- start sp-wrapper -->
    <link rel="stylesheet" href="/sb/stock.css"/>
    <div class="layui-tab">
        <ul class="layui-tab-title">
            <li ><a href="/stock_location/list">仓库管理</a></li>
            <li  class="layui-this"><a href="/stock_location/reservoir">库区管理</a></li>
            <li ><a href="/stock_location/shelf">仓位管理</a></li>
        </ul>
    </div>
    <div class="sp-wrapper">
<!--        <h2 class="sp-title"><span>库区管理</span></h2>-->
        <div class="sp-tool">
            <span class="fr"><i class="add" id="add">新增库区</i></span>
        </div>
        <div class="sp-table">
            <table>
                <thead>
                <tr>
                    <th>库区编号</th>
                    <th>库区名称</th>
                    <th>所属仓库</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <tr th:each="ReservoirEntity:${reservoir}">
                    <td th:text="${ReservoirEntity.getNumber()}"></td>
                    <td th:text="${ReservoirEntity.getName()}"></td>
                    <td th:text="${ReservoirEntity.getName1()}"></td>
                    <td>
                        <i th:attr="data-id=${ReservoirEntity.getId()},data-number=${ReservoirEntity.getNumber()},data-name=${ReservoirEntity.getName()},data-store_house=${ReservoirEntity.getParentId()}"
                           class="edit">修改</i>
                        <i th:attr="data-id=${ReservoirEntity.getId()}" class="delete">删除</i>
                    </td>
                </tr>
                </tbody>
            </table>
            <div class="layui-box layui-laypage layui-laypage-default pageBox">
                <paging:pager th:value="${pageIndex}" th:rows="${totalSize}" th:size="${pageSize}"/>
            </div>
        </div>
    </div>

    <!-- 模板 新增/修改 -->
    <script type="text/template" id="tpl-1">
        <div class="T-box">
            <p><span>所属仓库</span>
                <select id="fl-store_house">
                    <option selected value="0">选择仓库</option>
                    <option th:each="StoreHouseEntity:${houses}" th:value="${StoreHouseEntity.getId()}"
                            th:text="${StoreHouseEntity.getName()}"></option>
                </select>
            </p>
            <p><span>库区编号</span><input id="fl-number" type="text"/></p>
            <p><span>库区名称</span><input id="fl-name" type="text"/></p>
        </div>
    </script>
    <script type="text/javascript" src="/sb/jquery.min.js"></script>
    <script type="text/javascript" src="/sb/Tips.min.js"></script>
    <script type="text/javascript">
        $(function () {
            // 新增
            $('.add').click(function () {
                Tips.alert({
                    title: '新增库区',
                    content: $('#tpl-1').html(),
                    before: function () {
                        var number = $('#fl-number');
                        var name = $('#fl-name');
                        var store_house = $('#fl-store_house');
                        if (number.val() == '') {
                            alert("库区编码不能为空");
                            return false;
                        }
                        if (name.val() == '') {
                            alert("库区名称不能为空");
                            return false;
                        }
                        if (store_house.val() == ''|| store_house.val()==0) {
                            alert("所属仓库不能为空");
                            return false;
                        }

                        var dataObj = {
                            "name": name.val(),
                            "number": number.val(),
                            "store_house": store_house.val()
                        }

                        // ajax处理
                        $.ajax({
                            url: '/ajax_stock/add_reservoir',
                            type: "POST",
                            dataType: 'json',
                            contentType: 'application/json',
                            data: JSON.stringify(dataObj),
                            success: function (res) {
                                if (res.code == 0) {
                                    Tips.close();
                                    location.reload(true);
                                } else {
                                    alert(res.msg);
                                }
                            },
                            complete: function(XMLHttpRequest){
                                if (XMLHttpRequest.getResponseHeader("REDIRECT")=="NO_ACCESS")alert("无权限访问");
                            },
                        });
                        return false;
                    }
                });
            });

            // 修改
            $('.edit').click(function () {
                var data_id = $(this).attr('data-id');
                var data_number = $(this).attr('data-number');
                var data_name = $(this).attr('data-name');
                var data_store_house = $(this).attr('data-store_house');
                Tips.alert({
                    title: '修改库区',
                    content: $('#tpl-1').html(),
                    render: function () {
                        $('#fl-number').val(data_number);
                        $('#fl-name').val(data_name);
                        $('#fl-store_house').val(data_store_house);
                    },
                    before: function () {
                        var number = $('#fl-number');
                        var name = $('#fl-name');
                        var store_house = $('#fl-store_house');
                        if (number.val() == '') {
                            alert("库区编码不能为空");
                            return false;
                        }
                        if (name.val() == '') {
                            alert("库区名称不能为空");
                            return false;
                        }
                        if (store_house.val() == ''|| store_house.val()==0) {
                            alert("所属仓库不能为空");
                            return false;
                        }

                        var dataObj = {
                            "id": data_id,
                            "name": name.val(),
                            "number": number.val(),
                            "store_house": store_house.val()
                        }

                        // ajax处理
                        $.ajax({
                            url: '/ajax_stock/update_reservoir',
                            type: "POST",
                            dataType: 'json',
                            contentType: 'application/json',
                            data: JSON.stringify(dataObj),
                            success: function (res) {
                                if (res.code == 0) {
                                    Tips.close();
                                    location.reload(true);
                                } else {
                                    alert(res.msg);
                                }
                            },
                            complete: function(XMLHttpRequest){
                                if (XMLHttpRequest.getResponseHeader("REDIRECT")=="NO_ACCESS")alert("无权限访问");
                            },
                        });
                        return false;
                    }
                });
            });

            // 删除
            $('.delete').click(function () {
                var id = $(this).attr('data-id');
                Tips.confirm({
                    title: '删除库区',
                    content: '您确定要删除该库区吗？',
                    define: function () {
                        // ajax处理
                        // .. id
                        // ajax处理
                        $.ajax({
                            url: '/ajax_stock/del_stock_house',
                            type: "POST",
                            dataType: 'json',
                            contentType: 'application/json',
                            data: id,
                            success: function (res) {
                                Tips.alert(res.msg, function () {
                                    location.reload(true);
                                });
                            },
                            complete: function(XMLHttpRequest){
                                if (XMLHttpRequest.getResponseHeader("REDIRECT")=="NO_ACCESS")alert("无权限访问");
                            },
                        });
                    }
                });
            });
        });
    </script>
    <!-- end sp-wrapper -->

</div>
</body>
<html>
